package com.snow.useviewdemo.function;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import com.snow.useviewdemo.AlgorithmTest;
import com.snow.useviewdemo.R;
import com.snow.useviewdemo.base.BaseActivity;
import com.snow.useviewdemo.databinding.ActAlgorithmTestBinding;

import butterknife.OnClick;

/**
 * author : zyt
 * e-mail : 632105276@qq.com
 * date   : 2020-03-02
 * desc   :
 */
public class AlgorithmTestActivity extends BaseActivity<ActAlgorithmTestBinding> {
    private AlgorithmTest algorithmTest;
    int[] testArray = {10, 4, 13, 3, 23, 15, 17, 8, 20, 26, 6, 30, 18, 12};
    private int[] newArray;

    @Override
    protected int getLayoutId() {
        return R.layout.act_algorithm_test;
    }

    @Override
    protected void setViewData(Bundle savedInstanceState) {
        algorithmTest = new AlgorithmTest();
    }


    @OnClick({R.id.btn_01, R.id.btn_02, R.id.btn_03, R.id.btn_04, R.id.btn_05})
    public void onViewClicked(View view) {
        mBinding.sortText.setText("");
        switch (view.getId()) {
            case R.id.btn_01:
                newArray = algorithmTest.maoPaoSort(testArray);
                printlnArray();
                break;
            case R.id.btn_02:
                newArray = algorithmTest.selectSort(testArray);
                printlnArray();
                break;
            case R.id.btn_03:
                newArray = algorithmTest.insertionSort(testArray);
                printlnArray();
                break;
            case R.id.btn_04:
                byte a = 5;
                a += 0.5f;
                mBinding.sortText.setText("===" + (a));
                break;
            case R.id.btn_05:

                break;
        }
    }

    StringBuffer stringBuffer;
    int index = 0;

    private void printlnArray() {
        index = 0;
        stringBuffer = new StringBuffer();
        mHandler.sendEmptyMessage(1002);
    }

    Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 1002:
                    if (index < newArray.length) {
                        stringBuffer.append(newArray[index] + " , ");
                        mBinding.sortText.setText(stringBuffer.toString());
                        mHandler.sendEmptyMessageDelayed(1002, 200);
                        index++;
                    } else {
                        mHandler.removeMessages(1002);
                    }
                    break;
            }
            return false;
        }
    });
}
